//
//  ViewController.m
//  CameraDemo
//
//  Created by xujiangtao on 15/11/11.
//  Copyright © 2015年 xujiangtao. All rights reserved.
//

#import "ViewController.h"
#import "EditPhotoViewController.h"

@interface ViewController ()<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    UIImagePickerController* imagePicker;
}
@property (nonatomic,retain) UIView* cameraOverlay;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    UIButton* takePhotoBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    takePhotoBtn.frame = CGRectMake(50, 100, 100, 40);
    [takePhotoBtn setTitle:@"拍照" forState:UIControlStateNormal];
    [self.view addSubview:takePhotoBtn];
    
    [takePhotoBtn addTarget:self action:@selector(clickTackPhotoButton) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark- Click Event
- (void)clickTackPhotoButton{
    UIActionSheet* actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相册",@"拍照", nil];
    [actionSheet showInView:self.view];
}
- (void)clickTakeCamera{
    [imagePicker takePicture];
}
- (void)clickCancelTakePhoto{
    
}
#pragma mark- ActionSheet
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"button index = %d",(int)buttonIndex);
    imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    
//    imagePicker.allowsEditing = YES;
//    imagePicker.allowsImageEditing = YES;
    if (buttonIndex == 0) {
        //相册
        if ([UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary]) {
            imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentViewController:imagePicker animated:YES completion:^{
                
            }];
        }
    }else if (buttonIndex == 1){
        //拍照
        if ([UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera]) {
            imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
            imagePicker.showsCameraControls = NO;
//            self.cameraOverlay = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
//            self.cameraOverlay.backgroundColor = [UIColor redColor];
//            imagePicker.cameraOverlayView = self.cameraOverlay;
            imagePicker.cameraOverlayView = [self getOverLayView];
            
            [self presentViewController:imagePicker animated:YES completion:^{
                
            }];
        }
        
    }
}
- (UIView *)getOverLayView{
    UIView* overLayView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 100, self.view.frame.size.width, 100)];
    overLayView.userInteractionEnabled = YES;
    overLayView.backgroundColor = [UIColor whiteColor];
    //拍照
    UIButton* takePhotoBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [takePhotoBtn setTitle:@"拍照" forState:UIControlStateNormal];
    takePhotoBtn.frame = CGRectMake(10, 40, 100, 40);
    [overLayView addSubview:takePhotoBtn];
    [takePhotoBtn addTarget:self action:@selector(clickTakeCamera) forControlEvents:UIControlEventTouchUpInside];
    
    return overLayView;
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    NSLog(@"image picker delegate == %@",info);
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    
    EditPhotoViewController* editVC = [[EditPhotoViewController alloc] init];
    editVC.image = image;
//    UINavigationController* navigation = [[UINavigationController alloc] initWithRootViewController:editVC];
    
    [picker pushViewController:editVC animated:NO];
    
}

#pragma mark-
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
